// 获取当前url参数的公共方法    
function getUrlValue(key){
    // ex:?id=1&v=2
    var params = location.search;
    if(params.length>1){
        // ex:id=1&v=2
        params = location.search.substring(1);
        var paramArr = params.split("&");
        for(var i=0;i<paramArr.length;i++){
            var kv = paramArr[i].split("=");
            if(kv[0]==key){
                // 是我要查询的参数
                return kv[1];
            }
        }
    }
    return "";
}


$.ajaxSetup({
    // headers: {
    //     Authorization: localStorage.getItem("Authorization")
    // },
    beforeSend: function(xhr, settings) {  
        // 在这里添加全局的请求头  
        xhr.setRequestHeader('Authorization', localStorage.getItem("Authorization"));  
        // 例如，设置一个身份验证令牌  
        // xhr.setRequestHeader('Authorization', 'Bearer ' + token);  
    }
});
//注销
function logout() {
    if(confirm("确认注销？")) {
        jQuery.ajax({
            url: "/user/logout",
            type: "POST",
            data: {},
            success:function(result) {
                if(result != null && result.code == 200) {
                    location.href = "/login.html"; 
                } else {
                    alert("注销失败");
                }
            }
        });
    }
}

//删除
function myDel(id) {
    if(confirm("确认是否删除？")) {
        //删除文章
        jQuery.ajax({
            url: "/art/del",
            type: "POST",
            data: {"id":id},
            success:function(result) {
                if(result != null & result.code == 200 && result.data == 1) {
                    //删除成功
                    //刷新页面
                    location.href = location.href;
                }
            }
        });
    }
}

//显示个人信息
function showInfo(){
    jQuery.ajax({
        url: "/user/showinfo",
        type: "POST",
        data:{},
        success: function(result) {
            if(result != null && result.code==200) {
                if(result.data.nickname!=null) {
                    result.data.username = result.data.nickname;
                }
                jQuery("#username").text(result.data.username);
                jQuery("#artCount").text(result.data.artCount);
            } else if(result != null && result.code == -1) {
                alert("个人信息获取失败，请重新刷新~");
            } 
            
        }
    });
}

//个人列表 1 /草稿箱列表 0  
function getMyList(state) {
    jQuery.ajax({
        url: "/art/mylist",
        type: "POST",
        data:{"state":state},
        success: function(result) {
            if(result != null && result.code == 200) {
                //两种情况
                //有文章
                if(result.data != null && result.data.length>0) {
                    var artListDiv = "";
                    for(var i = 0;i<result.data.length;i++) {
                        var artItem = result.data[i];
                        artListDiv += '<div class="blog">';
                        artListDiv += '<div class="title">'+artItem.title+'</div>';
                        artListDiv += '<div class="date">'+artItem.updatetime+'</div>';
                        if(artItem.content.length > 200) {
                            artItem.content = artItem.content.substring(0,200);
                        }
                        artListDiv += '<div class="desc">'+artItem.content +'</div>';
                        if(state == 1) {
                            artListDiv += '<a href="/blog_content.html?id='+artItem.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
                        }
                        artListDiv += '<a href="/blog_edit.html?id='+artItem.id + '" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';
                        artListDiv += '<a href="javascript:myDel('+artItem.id + ');" class="detail">删除 &gt;&gt;</a>&nbsp;&nbsp;';
                        artListDiv += '</div>';
                    }
                    jQuery("#artDiv").html(artListDiv);
                }else {
                    //无文章
                    jQuery("#artDiv").html("<h3>暂无文章<h3>");
                }
            } else if(result != null && result.code == -1) {
                alert("查询文章列表出错，请重试~");
            } 
            
        }
    });
}

// //获取url中的数据
// function getUrlValue(key) {
//     var params = location.search;
//     if(params.length>1) {
//         params = location.search.substring(1);
//         var paramArr = params.split("&");
//         for(var i = 0;i<paramArr.length;i++) {
//             var kv = paramArr[i].split("=");
//             if(kv[0] = key) {
//                 return kv[1];
//             }
//         }
        
//     }
// return "";
// }


// 发布博客
function mysub() {
    // alert(editor.getValue()); // 获取值
    // editor.setValue("#123") // 设置值
    //1、判断标题和正文是由内容
    var title = jQuery("#title");
    if(title.val() == "") {
    alert("请先输入标题~");
        title.focus();
        return;
    }
    if(editor.getValue() == "") {
        alert("请先输入正文内容");
        return;
    }
    id = getUrlValue("id");
    if(confirm("确认提交？")) {
        //2、请求后端进行博客添加
        jQuery.ajax({
            url: "/art/add",
            type: "POST",
            data:{"id":id,"title":title.val(),"content":editor.getValue()},
            success:function(result) {
                if(result != null && result.code == 200 && result.data==1) {

                    location.href = "/myblog_list.html";
                } else {
                    alert("抱歉，添加文章失败,请重试~");
                }
            }
        });
        // alert(editor.getValue());//获取值
    }
}




//保存草稿
function mydraft() {
    //1、判断标题是否有内容
    var title = jQuery("#title");
    if(title.val() == "") {
        alert("请先输入标题，再进行保存~");
        return;
    }
    if(editor.getValue() == "") {
        if(!confirm("正文暂无内容，确定保存吗？")) {
            return;
        }
    }
    id = getUrlValue("id");
    //保存
    jQuery.ajax({
        url: "/art/draft",
        type:"POST",
        data:{"id":id,"title":title.val(),"content":editor.getValue()},
        success: function(result) {
            if(result != null && result.code == 200 && result.data.ret == 1) {
                if(result.data.id == -1) {
                    alert("保存成功");
                    location.href = "/mydraft_list.html";
                } else{
                    if(confirm("保存草稿成功，是否继续编辑？")) {
                        //刷新当前页面
                        location.href = "/blog_edit.html?id="+result.data.id;
                    } else {
                        location.href = "/mydraft_list.html";
                    }
                }
            } else {
                alert(result.msg);
            }
        }
    });
}

 //定时发布
        
 function mytiming() {
    //1、判断标题和正文是由内容
    var title = jQuery("#title");
    if(title.val() == "") {
        alert("请先输入标题~");
        title.focus();
        return;
    }
    if(editor.getValue() == "") {
        alert("请先输入正文内容");
        return;
    }
    //获取控件中的日期
    var day = jQuery("#day");
    var time = jQuery("#time");
    if(day.val() == "" || time.val() == "") {
        alert("请选择日期");
        return;
    }
    //如果存在id，说明是在修改曾经发布过的博客
    id = getUrlValue("id");
    //请求后端
    if(confirm("确认定时发布？")) {
        jQuery.ajax({
            url: "/art/mytiming",
            type:"POST",
            data:{"id":id,"day":day.val(),"time":time.val(),"title":title.val(),"content":editor.getValue()},
            success:function(result) {
                if(result != null && result.code == 200 && result.data == 1) {

                    location.href = "/myblog_list.html";
                } else {
                    alert("抱歉定时设置失败，请重试~");
                }
            }
        }); 
    }           
}

